<template>
  <view style="max-hight: 100vh">
    <scroll-view
      :refresher-enabled="isOpenRefresh"
      scroll-y="true"
      style="hight: 100vh"
      :refresher-triggered="triggered"
      @refresherpulling="onPulling"
      @refresherrefresh="onRefresh"
      @refresherrestore="onRestore"
      @refresherabort="onAbort"
      refresher-default-style="none"
      :refresher-threshold="0"
      scroll-anchoring="true"
    >
      <view v-if="show" class="loading">
        <!-- 加载中样式 -->
        <view class="loadingThree">
          <text></text>
          <text></text>
          <text></text>
          <text></text>
          <text></text>
        </view>
      </view>
      <!-- 释放刷新样式 -->
      <view v-if="show2" class="releaseLoading">
        <view class="text"> 释放刷新 </view>
      </view>
      <slot></slot>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      triggered: false, //当下下拉状态 true：启动刷新  false：刷新没有触发
      isOpenRefresh: true, // 是否开启下拉
      show: false,//加载中动画
      show2: false,// 释放刷新动画
    };
  },
  onLoad() {
    this._freshing = false;
    this.triggered = "restore"; // 需要重置
  },
  methods: {
    // 自定义下拉刷新控件被下拉
    onPulling(e) {
      if (e.detail.deltaY <= 0) {// 防止上滑页面也触发下拉
        return;
      } 
      if (e.detail.deltaY > 100) { // 下拉到指定位置 才触发刷新
        this.show2 = true;
      }
    },
    // 自定义下拉刷新被触发
    async onRefresh(e) {
      if (!this.show2) return; // 判断是否触发刷新
      this.show2 = false; // 触发刷新 关闭释放刷新动画
      if (this._freshing) return;
      this.triggered = true; // 启动刷新
      this._freshing = true; 
      this.show = true; // 加载中动画显示
      await this.$emit("refresh"); // 触发父组件刷新方法
    },
    closeRefresh() { // 关闭刷新  父组件调用
      this.triggered = "restore"; // 需要重置
      this.triggered = false; // 关闭刷新
      this._freshing = false; 
      this.show = false; // 加载动画关闭
    },
    // 自定义下拉刷新被复位
    onRestore() {
      this.triggered = "restore"; // 需要重置
    },
    // 自定义下拉刷新被中止
    onAbort() {
      this.triggered = "restore"; // 需要重置
      this._freshing = false;
      this.show2 = false;
    },
  },
};
</script>

<style lang="scss">
page {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  z-index: 99999;
}

.releaseLoading {
  height: 50rpx;
  z-index: 99999;
  // padding-bottom: 50rpx;
  background-color: rgba(0, 0, 0, 0);
  text-align: center;
  font-size: 32rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #8993a4;
  position: relative;

  .text {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
  }
}

.loading {
  z-index: 99999;
  padding-bottom: 50rpx;
  background-color: rgba(0, 0, 0, 0);
  height: 50rpx;
  position: relative;

  .loadingThree {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50rpx;
    background-color: rgba(0, 0, 0, 0);
  }

  .loadingThree text {
    display: inline-block;
    width: 15rpx;
    height: 15rpx;
    margin-right: 5rpx;
    border-radius: 50%;
    background: lightgreen;
    -webkit-animation: load 1.04s ease infinite;
  }

  .loadingThree text:last-child {
    margin-right: 0rpx;
  }

  @-webkit-keyframes load {
    0% {
      opacity: 1;
    }

    100% {
      opacity: 0;
    }
  }

  .loadingThree text:nth-child(1) {
    -webkit-animation-delay: 0.13s;
  }

  .loadingThree text:nth-child(2) {
    -webkit-animation-delay: 0.26s;
  }

  .loadingThree text:nth-child(3) {
    -webkit-animation-delay: 0.39s;
  }

  .loadingThree text:nth-child(4) {
    -webkit-animation-delay: 0.52s;
  }

  .loadingThree text:nth-child(5) {
    -webkit-animation-delay: 0.65s;
  }
}
</style>
